package com.pubinfo.passbook.common.config;

import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    
    //  super.configureMessageConverters(converters);
    
    converters.add(responseBodyConverter());
  }
  
  @Bean
  public MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin() {
    return new MasterSlaveAutoRoutingPlugin();
  }
  
  @Bean
  public MybatisPlusInterceptor paginationInterceptor() {
    MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
    
    //乐观锁
    mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    //分页配置
    mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return mybatisPlusInterceptor;
  }
  
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
  }
  
  @Bean
  public HttpMessageConverter<String> responseBodyConverter() {
    StringHttpMessageConverter converter = new StringHttpMessageConverter(
        StandardCharsets.UTF_8);
    return converter;
  }
  
  /**
   * 跨域支持
   *
   * @return
   */
//  @Bean
//  public WebMvcConfigurer corsConfigurer() {
//    return new WebMvcConfigurer() {
//      @Override
//      public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**").allowedMethods( new String[]{"GET", "POST", "PUT", "DELETE"}).allowedHeaders("*").allowCredentials(true).allowedOrigins("*")
//            //设置缓存时间，减少重复响应
//            .maxAge(3600);
//      }
//    };
//  }
  @Bean
  public CxmSqlInjector CxmSqlInjector() {
    return new CxmSqlInjector();
  }
  
  /**
   * 外部文件访问
   */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/template/**")
        .addResourceLocations("classpath:/file_template/");
    // 解决swagger无法访问
  
    registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    // 解决swagger的js文件无法访问
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }
//  @Bean
//  public HttpMessageConverters fastJsonHttpMessageConverters() {
//// 1、需要先定义一个 convert 转换消息的对象;
//    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//// 2、添加fastJson 的配置信息，比如：是否要格式化返回的json数据;
//    FastJsonConfig fastJsonConfig = new FastJsonConfig();
//    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//// 3、在convert中添加配置信息.
//    fastConverter.setFastJsonConfig(fastJsonConfig);
//    HttpMessageConverter<?> converter = fastConverter.;
//    return new HttpMessageConverters(converter);
//  }
}
